/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.Interface
 * @since April 2004
 */
public abstract class SimpleDialog extends JComponent implements ActionListener {
	
	public static final int OK_OPTION = 0;
    public static final int CANCEL_OPTION = 1;
	
	private int command;
	private JPanel mainPane = new JPanel();
	private JDialog dlg;
	private JPanel confirmationPane = new JPanel();
    private JButton ok = new JButton("OK");
    private JButton cancel = new JButton("Cancel");
	
//*****************************************************************************
	
	/** Shows this dialog.
	* @param parent This dialog's parent
	* @param title The title of this dialog
	*/
	public int showDialog(JInternalFrame parent, String title) {
		dlg = new JDialog();
		dlg.setModal(true);
		dlg.setTitle(title);
		dlg.setSize(new Dimension(300, 200));
		
		dlg.setLocation(300,200);
		
		dlg.setResizable(false);
        dlg.getContentPane().setLayout(new BorderLayout());
        dlg.getContentPane().add(mainPane, BorderLayout.CENTER);
        dlg.getContentPane().add(confirmationPane, BorderLayout.SOUTH);
        dlg.getRootPane().setDefaultButton(ok);
		
        //Sets up confirmation pane
        confirmationPane.add(ok);
        ok.addActionListener(this);
		
        cancel.setMnemonic(KeyEvent.VK_C);
        confirmationPane.add(cancel);
        cancel.addActionListener(this);
        
		dlg.pack();
        dlg.setVisible(true);
        return command;
    }
	
	public int showDialog(DesktopPostp parent, String title) {
		dlg = new JDialog(parent, title, true);
		dlg.setSize(new Dimension(300, 200));
		dlg.setLocationRelativeTo(parent);
		dlg.setResizable(false);
        dlg.getContentPane().setLayout(new BorderLayout());
        dlg.getContentPane().add(mainPane, BorderLayout.CENTER);
        dlg.getContentPane().add(confirmationPane, BorderLayout.SOUTH);
        dlg.getRootPane().setDefaultButton(ok);
		
        //Sets up confirmation pane
        confirmationPane.add(ok);
        ok.addActionListener(this);
		
        cancel.setMnemonic(KeyEvent.VK_C);
        confirmationPane.add(cancel);
        cancel.addActionListener(this);
        
		dlg.pack();
        dlg.setVisible(true);
        return command;
    }
	
//*****************************************************************************
	
	/** Implements method actionPerformed(ActionEvent e) of ActionListener class.
	* @see ActionListener
	*/
	public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton)e.getSource();
        if (btn.getActionCommand() == "OK"){
            command = OK_OPTION;
            approvedPerform();
        }else{
            command = CANCEL_OPTION;
        }
        dlg.setVisible(false);
    }
	
//*****************************************************************************
	
	/** Does something when OK button is pressed. */
    public abstract void approvedPerform();
	
//*****************************************************************************
	
	public void setPane(JPanel pane) {
		mainPane = pane;
	}
	
//*****************************************************************************
	
	public void setButtonsEnabled(boolean val) {
		ok.setEnabled(val);
		cancel.setEnabled(val);
	}
	
//*****************************************************************************
}
